/**
 *	方案组协助申请审批通过后，自动把方案组（方案组由管理员自建并手动往组内添加用户）申请中的用户填加到业务机会小组中。
 *	created by Harry 2016年12月06日16:26:10
 */
public without sharing class AddOppMemberHandler implements Triggers.Handler{
    public void handle(){
    	List<OpportunityTeamMember> otmList = new List<OpportunityTeamMember>();
    	List<PlanGroupAssist__c> pgaList = (List<PlanGroupAssist__c>)Trigger.new;

        //查询公共小组成员
        List<GroupMember> gmList = [Select g.UserOrGroupId From GroupMember g where g.Group.DeveloperName='OppCooperationTeam'];
    	for(PlanGroupAssist__c pga : pgaList){
            //案组协助申请审批通过之后才能加人
    		if(null!=pga.Opportunity__c && pga.ApprovalStatus__c== '审核成功'){
                for(GroupMember gm : gmList ){
                    OpportunityTeamMember otm = new OpportunityTeamMember();
                    otm.OpportunityId = pga.Opportunity__c;
                    otm.UserId = gm.UserOrGroupId;
                    otm.TeamMemberRole='Pre-Sales Consultant';
        			otmList.add(otm);
                    ConnectApi.FeedElement fi = ConnectApiHelper.postFeedItemWithMentions(
                        Network.getNetworkId(), pga.Opportunity__c,
                        '{'+gm.UserOrGroupId+'}, 请协助跟进业务机会');
                }
    		}
    	}
		insert otmList;
    }
}